//
//  News24HoursTableViewCell.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/1.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "News24HoursTableViewCell.h"
#import "NetEaseImgView.h"
#import "UIImageView+WebCache.h"

@implementation News24HoursTableViewCell {
    //图片
    NetEaseImgView* imgView;
    
    //标题
    UILabel* titleLabel;
    
    //新闻简介
    UILabel* contentLabel;
    
    //跟帖
    UIImageView* replyImgView;
    UILabel* replyLabel;
    
    //新闻序号，前三位才有，一个三角形的标志
    UIImageView* newsTagImgView;
    UILabel* newsTagLabel;          //新闻标记，1、2、3
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        imgView = [[NetEaseImgView alloc] init];
        [self.contentView addSubview:imgView];
        
        titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.font = [UIFont systemFontOfSize:16];
        titleLabel.numberOfLines = 0;
        [self.contentView addSubview:titleLabel];
        
        contentLabel = [[UILabel alloc] init];
        contentLabel.textColor = [UIColor grayColor];
        contentLabel.numberOfLines = 0;
        contentLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:contentLabel];
        
        replyImgView = [[UIImageView alloc] init];
        UIImage* image = [UIImage imageNamed:@"cola_bubble_gray"];
        [image stretchableImageWithLeftCapWidth:10 topCapHeight:0];
        replyImgView.image = image;
        [self.contentView addSubview:replyImgView];
        
        replyLabel = [[UILabel alloc] init];
        replyLabel.font = [UIFont systemFontOfSize:10];
        replyLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:replyLabel];
        
        newsTagImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        newsTagImgView.image = [UIImage imageNamed:@"todaynews_cell_tag"];
        [self.contentView addSubview:newsTagImgView];
        
        newsTagLabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 2, 15, 15)];
        newsTagLabel.textColor = [UIColor whiteColor];
        newsTagLabel.font = [UIFont systemFontOfSize:12];
        newsTagLabel.textAlignment = NSTextAlignmentCenter;
        [newsTagImgView addSubview:newsTagLabel];
    }
    
    return self;
}

- (void)setNewsModel:(NewsModel *)newsModel {
    _newsModel = newsModel;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    //如果是前三名的新闻，就有一个标记
    if (_newsTag == 0 || _newsTag == 1 || _newsTag == 2) {
        newsTagImgView.hidden = NO;
        newsTagLabel.text = [NSString stringWithFormat:@"%li", _newsTag+1];
    } else {
        newsTagImgView.hidden = YES;
    }
    
    
    if (_newsTag == 0) {        //如果是第一条，排版也会不一样
        //图片
        [imgView setFrame:CGRectMake(10, 10, kWidth-20, self.bounds.size.height-90)];
        //标题
        [titleLabel setFrame:CGRectMake(10, self.bounds.size.height-75, kWidth-20, 30)];
        //新闻简介
        [contentLabel setFrame:CGRectMake(10, self.bounds.size.height-50, kWidth-20, 50)];
        
    } else {
        CGFloat imgWidth = 80;
        //图片
        [imgView setFrame:CGRectMake(10, 10, imgWidth, self.bounds.size.height-20)];
        //标题
        [titleLabel setFrame:CGRectMake(20+imgWidth, 10, kWidth-20-imgWidth-10, self.bounds.size.height/2-10)];
        //新闻简介
        [contentLabel setFrame:CGRectMake(20+imgWidth, self.bounds.size.height/2, kWidth-20-imgWidth-10, self.bounds.size.height/2-10)];
    }
    
    
    //公共的设置
    //图片内容
    NSString* imgUrl = _newsModel.imgUrl;
    if (imgUrl.length) {
        [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
    }
    
    //新闻标题
    titleLabel.text = _newsModel.title;
    //新闻简介
//    contentLabel.text = _newsModel.content;
    NSString *contentStr = _newsModel.content;
    if (contentStr.length) {
        NSAttributedString* contentAttrStr = [Tools attributedStrFromStr:contentStr lineSpace:3];
        contentLabel.attributedText = contentAttrStr;
        [contentLabel sizeToFit];
    }
   
    
    //跟帖的位置和内容
    [replyImgView setFrame:CGRectMake(kWidth-60, self.bounds.size.height-25, 50, 15)];
    [replyLabel setFrame:replyImgView.frame];
    float replyCount = [_newsModel.replyCount floatValue];
    if (replyCount>9999) {
        replyLabel.text = [NSString stringWithFormat:@"%.1f万跟帖", replyCount/10000];
    } else {
        replyLabel.text = [NSString stringWithFormat:@"%.0f跟帖", replyCount];
    }
}

@end
